package com.isdust.zor.core.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Log;

import com.isdust.zor.CoreConfig;

/**
 * 数据加密存储到本地
 * Created by zor on 2016/10/8.
 */

public class PreferenceHelper {

    private Context context;

    public PreferenceHelper(Context context) {
        this.context = context;
    }

    public void write(String k, Object v) {
        String res = CyptoUtils.encode(CoreConfig.key, String.valueOf(v));
        SharedPreferences preference = context.getSharedPreferences(CoreConfig.SP,
                Context.MODE_PRIVATE);
        Editor editor = preference.edit();
        editor.putString(k, res);
        Log.i("testwrite",res);
        editor.apply();
    }

    public String read(String k) {
        SharedPreferences preference = context.getSharedPreferences(CoreConfig.SP,
                Context.MODE_PRIVATE);
        return CyptoUtils.decode(CoreConfig.key, preference.getString(k, ""));
    }

    public void remove(String k) {
        SharedPreferences preference = context.getSharedPreferences(CoreConfig.SP,
                Context.MODE_PRIVATE);
        Editor editor = preference.edit();
        editor.remove(k);
        editor.apply();
    }

    public void clean() {
        SharedPreferences preference = context.getSharedPreferences(CoreConfig.SP,
                Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preference.edit();
        editor.clear();
        editor.apply();
    }
}